package cn.icare.cms.controller.restful;

import cn.icare.cms.MoneyOutService;
import cn.icare.cms.aop.UserLog;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/moneyout")
@Api(tags = "REST退款")
public class MoneyOutController extends BaseController {
    @Resource
    private MoneyOutService moneyOutService;
    private final String FALL_STRING = "请求失败,服务繁忙!";

    /**
     * URL:http://{ip}:{port}/rest/moneyOut
     * method:POST
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/updatestatus")
    @HystrixCommand(fallbackMethod = "updateMoneyOutStatusFall")
    @UserLog(desc = "更新退款审核状态")
    @ApiOperation(value = "更新退款审核状态", notes = "更新退款审核状态")
    public ResponseEntity<RespMsgVO> updateStatus(@RequestParam(name = "id") Long id,
                                                         @RequestParam("status")Integer status) {
        if (this.moneyOutService.updateStatus(id, status)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }


    /**
     * URL:http://{ip}:{port}/rest/moneyOut?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @HystrixCommand(fallbackMethod = "getListFall")
    @ApiOperation(value = "获取退款列表", notes = "获取退款列表")
    public ResponseEntity<RespMsgVO> getList(
            @RequestParam(name = "code", required = false) String  code
            ,@RequestParam(name = "orderCode", required = false) String  orderCode
            ,@RequestParam(name = "memberName", required = false) String  memberName
            ,@RequestParam(name = "status", required = false) Integer  status
            ,@RequestParam(name = "pageNum", required = false) Integer  pageNum
            ,@RequestParam(name = "pageSize", required = false) Integer  pageSize
    ) {
        return super.getRespOk(this.moneyOutService.getList(code,orderCode,memberName
        ,status,pageNum,pageSize));
    }




    //region fallback methods

    private ResponseEntity<RespMsgVO> updateMoneyOutStatusFall(Long id, Integer status) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getListFall(
            String code, String orderCode,String memberName
            ,Integer status, Integer pageNum, Integer pageSize
    ) {
        return super.getRespFail(this.FALL_STRING);
    }

    //endregion
}
